import { _decorator, animation, Button, Collider2D, color, Color, Component, Contact2DType, Label, Node, ProgressBar, randomRange, randomRangeInt, Scheduler, Sprite, tween, UIOpacity, Vec3 } from 'cc';
import { BattleManager } from './BattleManager';
import { EntityManager } from './Base/EntityManager';

import { EEvent, EGroup, EState, EntityType, PLAY_AREA, EBuff, EPlayerType, EWuxing, EColor, EPet } from './Config/Enum';
import { Core } from './Core/Core';
import { EnemyStateMachine } from './EnemyStateMachine';
import { WallManager } from './WallManager';
import ObjectPoolManager from './Core/ObjectPoolManager';

import { NumberText } from './Core/NumberText';
import { EnemyConfig } from './Config/EnemyConfig';
import { EnemyBuffManager } from './BuffManager';

import { PetConfig } from './Config/PetConfig';
import { PetStateMachine } from './PetStateMachine';

const { ccclass, property } = _decorator;

@ccclass('PetManager')
export class PetManager extends EntityManager {
        // 添加缩放相关属性

    id: number;

    level: number;
    exp: number;
    expToNextLevel: number;
    assigned: boolean; // 是否被分配

    private _speedFactor: number = 1; // 添加速度因子

    //节点
    sp: Sprite = null!;
    ndHalo: Node = null!;
    // 状态
    // isMoving = true
    canAttack = true
    // isCounted = false;
    // isElite = false;
    // isBoss = false;


    stateMachine: EnemyStateMachine;



    // 属性
    ap: number;
    dp: number;
    speed = 100;
    attackSpeed = 100;
    hp = 100;
    maxHp = 100;
    evasion = 0.1;
    attackTarget: Collider2D;
    dropRate = 0.1;

    ndHp: Node = null!;
    spWuxing: Sprite = null!;
    wuxing: EWuxing;
    accuracy: number = 1;
    originalAccuracy: number = 1;


    initState(petType: EPet){
        if (!this.node.getComponent(PetStateMachine)) {
            this.fsm = this.node.addComponent(PetStateMachine);
        } else {
            this.fsm = this.node.getComponent(PetStateMachine)!;
        }


        this.fsm.init(petType);
        this.state = EState.IdleFront;

    }
    

    init(petType: EPet) {

        // this.spWuxing = this.ndHp.getChildByName("Wuxing").getComponent(Sprite);
        // this.sp = this.node.getChildByName('Animation').getComponent(Sprite);




        //如果节点没有EnemyStateMachine组件，再this.node.addComponent(EnemyStateMachine)
        if (!this.node.getComponent(PetStateMachine)) {
            this.fsm = this.node.addComponent(PetStateMachine);
        } else {
            this.fsm = this.node.getComponent(PetStateMachine)!;
        }


        this.fsm.init(petType);
        this.state = EState.Idle;

        // 设置基础属性
        //const petConfig = PetConfig[petType];


        //this.wuxing = PetConfig.wuxing;
        //this.spWuxing.color = color().fromHEX(EColor[this.wuxing]);

    }


    

    onLoad(): void {

    }

    protected onEnable(): void {
        this.stateMachine = this.node.getComponent(EnemyStateMachine);
        this.node.on("EnemyDieAnimationFinished", this.onDieAnimationFinished, this);
        this.node.on("EnemyAttackAnimationFinished", this.onAttackAnimationFinished, this);

    }

    protected onDisable(): void {
        this.node.off("EnemyDieAnimationFinished", this.onDieAnimationFinished, this);
        this.node.off("EnemyAttackAnimationFinished", this.onAttackAnimationFinished, this);
    }

    onDieAnimationFinished() {
        ObjectPoolManager.Instance.ret(this.node);
    }

    onAttackAnimationFinished() {
        this.state = EState.Idle;
        this.canAttack = true;
    }

    start() {
    }

    update(dt: number) {
        //console.log("scaleaaaaaa this.node.position:", this.node.position);
        // 应用速度因子
        const scaledDt = dt * this._speedFactor;

    }




        // 添加设置速度的方法
    setSpeedFactor(factor: number) {
        this._speedFactor = factor;
    }

    levelUp() {
        this.level++;
        this.node.setScale(Math.abs(this.node.scale.x) + 0.01, this.node.scale.y + 0.01, 1);
        this.speed += 0.1;

    }

    public getLevel(): number {
        return this.level;
    }
    


    attack() {
        if (!this.canAttack || !this.attackTarget) return;
        
        //this.isMoving = false;
        this.canAttack = false;
        this.state = EState.Attack;
        let damage = this.ap;
        // 计算城墙防御和闪避
        let wm = this.attackTarget.node.getComponent(WallManager);
                // 检查是否命中
        let isHit = Math.random() < this.accuracy - wm.dodge; 
        if (!isHit) {
            NumberText.show("miss", wm.node.worldPosition, Color.GRAY);
            return;
        }else{

            // 命中，计算伤害
            damage = damage*(1-wm.dp/(wm.dp+100))
            if (damage <= 0) {
                damage = 0; // 最少造成1点伤害
            }
        }
        this.attackTarget.node.getComponent(WallManager).hurt(damage);
    }



}

